package poradnik.medyczny.Model;

import org.json.JSONException;
import org.json.JSONObject;

import poradnik.medyczny.DB.DBAdapter;
import android.content.ContentValues;
import android.content.Context;

public class Likehood {
	int likehood;
	int countedLikehood;
	long id;
	long symptomId;
	long diseaseId;
	long localiztionId;

	// if not used id stays 0
	public boolean Save(Context context, DBAdapter.tables type) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();
		boolean successful = false;
		ContentValues values = new ContentValues();
		switch (type) {
		case LocalizationDiseaseLikehoods:// dis loc
			values.put(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_LOCALIZATION_ID, localiztionId);
			values.put(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_DISEASE_ID, diseaseId);
			values.put(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_LIKEHOOD, likehood);
			values.put(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_ID, id);

			successful = medical_db.update(DBAdapter.TABLE_LOCALIZATION_DISEASE_LIKEHOODS, values, DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_ID + "='" + id + "'", null);
			if (!successful) {
				id = medical_db.insert(DBAdapter.TABLE_LOCALIZATION_DISEASE_LIKEHOODS, values);
				successful = id == 0 ? false : true;
			}
			break;
		case SymptomLocalizationLikehoods:// loc sym
			values.put(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_LOCALIZATION_ID, localiztionId);
			values.put(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_SYMPTOM_ID, symptomId);
			values.put(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_LIKEHOOD, likehood);
			values.put(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_ID, id);

			successful = medical_db.update(DBAdapter.TABLE_SYMPTOM_LOCALIZATION_LIKEHOODS, values, DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_ID + "='" + id + "'", null);
			if (!successful) {
				id = medical_db.insert(DBAdapter.TABLE_SYMPTOM_LOCALIZATION_LIKEHOODS, values);
				successful = id == 0 ? false : true;
			}
			break;
		case SymptomDiseaseLikehoods:// sym di
			values.put(DBAdapter.SYMPTOM_DISEASE_LIKEHOODS_SYMPTOM_ID, symptomId);
			values.put(DBAdapter.SYMPTOM_DISEASE_LIKEHOODS_DISEASE_ID, diseaseId);
			values.put(DBAdapter.SYMPTOM_DISEASE_LIKEHOODS_LIKEHOOD, likehood);
			values.put(DBAdapter.SYMPTOM_DISEASE_LIKEHOODS_ID, id);

			successful = medical_db.update(DBAdapter.TABLE_SYMPTOM_DISEASE_LIKEHOODS, values, DBAdapter.SYMPTOM_DISEASE_LIKEHOODS_ID + "='" + id + "'", null);
			if (!successful) {
				id = medical_db.insert(DBAdapter.TABLE_SYMPTOM_DISEASE_LIKEHOODS, values);
				successful = id == 0 ? false : true;
			}
			break;
		}

		medical_db.close();

		return successful;
	}

	public int getLikehood() {
		return likehood;
	}

	public static Likehood parseJSON(JSONObject jsonObject, DBAdapter.tables type) throws JSONException {
		Likehood like = new Likehood();
		switch (type) {
		case LocalizationDiseaseLikehoods:// dis loc
			like.localiztionId = jsonObject.getLong("localization_id");
			like.diseaseId = jsonObject.getLong("disease_id");
			like.likehood = jsonObject.getInt("likehood");
			like.id = jsonObject.getLong("id");
			break;
		case SymptomLocalizationLikehoods:// loc sym
			like.localiztionId = jsonObject.getLong("localization_id");
			like.symptomId = jsonObject.getLong("symptom_id");
			like.likehood = jsonObject.getInt("likehood");
			like.id = jsonObject.getLong("id");
			break;
		case SymptomDiseaseLikehoods:// sym di
			like.symptomId = jsonObject.getLong("symptom_id");
			like.diseaseId = jsonObject.getLong("disease_id");
			like.likehood = jsonObject.getInt("likehood");
			like.id = jsonObject.getLong("id");
			break;
		}
		return like;

	}

	public void setLikehood(int likehood) {
		this.likehood = likehood;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getSymptomId() {
		return symptomId;
	}

	public void setSymptomId(long symptomId) {
		this.symptomId = symptomId;
	}

	public long getDiseaseId() {
		return diseaseId;
	}

	public void setDiseaseId(long diseaseId) {
		this.diseaseId = diseaseId;
	}

	public long getLocaliztionId() {
		return localiztionId;
	}

	public void setLocaliztionId(long localiztionId) {
		this.localiztionId = localiztionId;
	}

	public int getCountedLikehood() {
		return countedLikehood;
	}

	public void setCountedLikehood(int countedLikehood) {
		this.countedLikehood = countedLikehood;
	}
}
